<template>
  <div class="Department">
    <div class="text">部门退料</div>
    <div class="form">
      <!-- 搜索 -->
      <el-form :inline="true" class="demo-form-inline" style="text-align: left">
        <div class="form-top">
          <!-- 时间 -->
          <el-form-item label="开始时间">
            <el-col :span="11" style="width: 300px; padding: 0">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="form.date1"
                style="width: 100%"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="结束时间">
            <el-col :span="11" style="width: 300px; padding: 0">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="form.date2"
                style="width: 100%"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <div
            style="width: 300px; display: flex; justify-content: space-around"
          >
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-zoom-in"
                size="mediu"
                @click="onSearch"
                >查询</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button
                type="success"
                icon="el-icon-plus"
                size="mediu"
                @click="toAdd"
                >新增</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button
                type="info"
                icon="el-icon-refresh-right"
                size="mediu"
                @click="onRest"
                >重置</el-button
              >
            </el-form-item>
          </div>
        </div>
        <div class="form-bot">
          <!-- 领用部门 -->
          <el-form-item label="领用部门">
            <el-select
              v-model="form.pdeptId"
              placeholder="请选择活动区域"
              style="width: 300px; padding: 0"
            >
              <el-option
                :key="v.pdeptId"
                v-for="v in select1"
                :label="v.pdept"
                :value="v.pdeptId"
              ></el-option>
            </el-select>
          </el-form-item>

          <!-- 审核状态 -->
          <el-form-item label="审核状态">
            <el-select
              v-model="form.auditStatus"
              placeholder="请选择活动区域"
              style="width: 300px; padding: 0"
            >
              <el-option
                :key="v"
                v-for="v in select2"
                :label="
                  v == 1
                    ? '待审核'
                    : v == 2
                    ? '审核通过'
                    : v == 3
                    ? '拒绝审核'
                    : '--'
                "
                :value="v"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 搜索 -->
          <el-form-item>
            <el-input
              v-model="returnNo"
              placeholder="根据ID搜索"
              style="width: 300px"
            ></el-input>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 表单数据 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
      id="out_table"
    >
      <el-table-column
        label="退料日期"
        width="120"
        align="center"
        prop="returnDate"
      >
        <template slot-scope="scope">{{
          switchDate(scope.row.returnDate)
        }}</template>
      </el-table-column>
      <el-table-column
        prop="returnNo"
        label="退料单号"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="personalDept"
        label="退料部门"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="preparedBy"
        label="制单人"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="auditStatus"
        label="审核状态"
        show-overflow-tooltip
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.auditStatus == 2
            ? "审核通过"
            : scope.row.auditStatus == 3
            ? "拒绝审核"
            : scope.row.auditStatus == 1
            ? "待审核"
            : "---"
        }}</template>
      </el-table-column>
      <el-table-column
        prop="reviewedBy"
        label="审核人"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="140" align="center">
        <template slot-scope="scope">
          <el-button
            class="edit_btn"
            type="primary"
            icon="el-icon-edit-outline"
            @click="openEdit(scope.row)"
            size="small"
          >
          </el-button>
          <el-button
            class="del_btn"
            type="primary"
            icon="el-icon-delete"
            @click="delInfo(scope.row.returnNo)"
            size="small"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <div class="pagination">
      <div>
        <el-button
          class="down_btn"
          type="primary"
          icon="el-icon-download"
          @click="exportExcel()"
          size="small"
          >导出
        </el-button>
      </div>
      <div style="display: flex; align-items: center; margin-left: 300px">
        <span style="font-size: 13px; font-weight: bold"
          >共{{ Math.ceil(total / limit) }}页/{{ total }}条数据</span
        >

        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="page"
            :page-size="limit"
            layout="prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import FileSaver from "file-saver";
import * as XLSX from "xlsx";
export default {
  data: function () {
    return {
      tableData: [],
      form: {
        date1: "",
        date2: "",
        auditStatus: "",
        pdeptId: "",
      },
     
      returnNo: "",
      addDialog: false,
      page: 1,
      limit: 5,
      total: 0,
      select1: [],
      select2: [1,2,3],
    };
  },
  methods: {
    //渲染
    getData: function () {
      this.$axios({
        url: "/WReturnManagement/list",
        method: "post",
        data: {
          auditStatus: this.form.auditStatus,
          endTime: this.form.date2,
          limit: this.limit,
          page: this.page,
          pdeptId: this.form.pdeptId,
          returnNo: this.returnNo,
          startTime: this.form.date1,
        },
      }).then(({ data }) => {
        this.tableData = data.data;
        this.total = data.total;
      });
    },
    //搜索
    onSearch: function () {
      this.getData();
    },
    //重置
    onRest: function () {
      this.form.pdeptId = "";
      this.returnNo = "";
      this.form.date1 = "";
      this.form.auditStatus = "";
      this.form.date2 = "";
      this.page=1
      this.getData();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    },
    //删除
    delInfo: function (row) {
      // console.log(row);
      this.$axios({
        url: "/WReturnManagement/delete",
        method: "delete",
        params: {
          returnNo: row,
        },
      }).then(({ data }) => {
        if (data.code == 0) {
          this.$message.success("删除成功！");
          this.getData();
        } else {
          this.$message.error("删除失败！请重新删除！");
        }
      });
    },
    //编辑
    openEdit: function (row) {
      this.$router.push("/home/Bmtledit");
      sessionStorage.setItem("Material", JSON.stringify(row));
      this.$store.state.xjqVuex.Material = row;
    },
    toAdd: function () {
      this.$router.push("/home/BmtlAdd");
    },
    //时间转换
    switchDate: function (sj) {
      var now = new Date(sj);
      var year = now.getFullYear();
      var month = now.getMonth() + 1;
      var date = now.getDate();
      return year + "-" + month + "-" + date;
    },
    //导出
    exportExcel() {
      let wb = XLSX.utils.table_to_book(document.querySelector("#out_table"));
      let wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream" }),
          "部门退料.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },
    //渲染下拉
    getSelect: function () {
      this.$axios({
        url: "/personal/pdept",
      }).then(({ data }) => {
        this.select1 = data.data;
      });
      // this.$axios({
      //   url: "/wReceiptManagement/status",
      //   method: "post",
      // }).then(({ data }) => {
      //   this.select2.push(data.data[0].auditStatus);
      //   this.select2.push(data.data[1].auditStatus);
      //   this.select2.push(data.data[3].auditStatus);
      // });
    },
  },
  mounted: function () {
    this.getData();
    this.getSelect();
  },
};
</script>
<style lang="less" scoped>
.Department {
  padding: 30px;
}
.text {
  font-weight: 700;
  font-style: normal;
  font-size: 24px;
  color: #344563;
  margin-bottom: 20px;
}
.form {
  display: flex;
  flex-direction: column;
}
.form-top {
  display: flex;
  justify-content: space-around;
}
.form-bot {
  display: flex;
  justify-content: space-around;
}
.del_btn {
  font-size: 14px;
  color: #ffc5c5;
  background: #fef3f3;
  border-color: #fef3f3;
}

.edit_btn {
  font-size: 14px;
  color: #5fa4e2;
  background: #eef6fc;
  border-color: #eef6fc;
}
.down_btn {
  font-size: 14px;
  color: #64c8bc;
  background: #eef6fc;
  border-color: #eef6fc;
}
.pagination {
  display: flex;
  margin-top: 50px;
  justify-content: space-between;
  margin-right: 20px;
}
</style>
